package com.livestocksolutions.ui;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.FlowLayout;

import javax.swing.BoxLayout;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Font;

import com.livestocksolutions.Bovine;
import com.livestocksolutions.LivestockSolutions;
import com.livestocksolutions.util.JDatePickerDate;
import com.livestocksolutions.util.Table;

/**
 * Generate a report showing the Parents and offspring of the selected Bovine.
 * @author Matt Lang
 *
 * @.modified 23 October 2012
 */
public class PedigreePanel extends JPanel 
{
	
	private static final long serialVersionUID = 1L;
	private JTextField bovineIdTxt;
	JPanel offspringDisplay = new JPanel();
	JLabel sireDisplay = new JLabel("");
	JLabel displayVisTag = new JLabel("");
	JLabel displayNLIStag = new JLabel("");
	JLabel dameDisplay = new JLabel("");
	
	/**
	 * Constructor.
	 */
	public PedigreePanel() {
		setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		add(panel, BorderLayout.NORTH);
		panel.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_2 = new JPanel();
		panel.add(panel_2, BorderLayout.CENTER);
		
		bovineIdTxt = new JTextField();
		panel_2.add(bovineIdTxt);
		bovineIdTxt.setColumns(15);
		
		JButton search = new JButton("Show Offspring");
		panel_2.add(search);
		
		JPanel panel_1 = new JPanel();
		add(panel_1, BorderLayout.CENTER);
		panel_1.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_6 = new JPanel();
		panel_1.add(panel_6, BorderLayout.CENTER);
		panel_6.setLayout(new BoxLayout(panel_6, BoxLayout.Y_AXIS));
		
		JPanel panel_3 = new JPanel();
		panel_6.add(panel_3);
		GridBagLayout gbl_panel_3 = new GridBagLayout();
		gbl_panel_3.columnWidths = new int[]{210, 239, 0};
		gbl_panel_3.rowHeights = new int[]{24, 0};
		gbl_panel_3.columnWeights = new double[]{1.0, 1.0, Double.MIN_VALUE};
		gbl_panel_3.rowWeights = new double[]{1.0, Double.MIN_VALUE};
		panel_3.setLayout(gbl_panel_3);
		
		JPanel panel_4 = new JPanel();
		GridBagConstraints gbc_panel_4 = new GridBagConstraints();
		gbc_panel_4.insets = new Insets(0, 0, 0, 5);
		gbc_panel_4.fill = GridBagConstraints.BOTH;
		gbc_panel_4.gridx = 0;
		gbc_panel_4.gridy = 0;
		panel_3.add(panel_4, gbc_panel_4);
		panel_4.setLayout(new BoxLayout(panel_4, BoxLayout.Y_AXIS));
		
		JLabel lblVisualTag = new JLabel("Visual Tag:");
		lblVisualTag.setFont(new Font("Tahoma", Font.BOLD, 20));
		panel_4.add(lblVisualTag);
		
		JLabel lblNlisTag = new JLabel("NLIS Tag:");
		lblNlisTag.setFont(new Font("Tahoma", Font.BOLD, 20));
		panel_4.add(lblNlisTag);
		
		JPanel panel_5 = new JPanel();
		GridBagConstraints gbc_panel_5 = new GridBagConstraints();
		gbc_panel_5.fill = GridBagConstraints.BOTH;
		gbc_panel_5.gridx = 1;
		gbc_panel_5.gridy = 0;
		panel_3.add(panel_5, gbc_panel_5);
		panel_5.setLayout(new BoxLayout(panel_5, BoxLayout.Y_AXIS));
		
		displayVisTag.setFont(new Font("Tahoma", Font.PLAIN, 20));
		panel_5.add(displayVisTag);
		
		displayNLIStag.setFont(new Font("Tahoma", Font.PLAIN, 20));
		panel_5.add(displayNLIStag);
		
		JPanel panel_7 = new JPanel();
		panel_6.add(panel_7);
		GridBagLayout gbl_panel_7 = new GridBagLayout();
		gbl_panel_7.columnWidths = new int[]{210, 239, 0};
		gbl_panel_7.rowHeights = new int[]{24, 0};
		gbl_panel_7.columnWeights = new double[]{1.0, 1.0, Double.MIN_VALUE};
		gbl_panel_7.rowWeights = new double[]{0.0, Double.MIN_VALUE};
		panel_7.setLayout(gbl_panel_7);
		
		JPanel panel_8 = new JPanel();
		GridBagConstraints gbc_panel_8 = new GridBagConstraints();
		gbc_panel_8.anchor = GridBagConstraints.NORTHWEST;
		gbc_panel_8.insets = new Insets(0, 0, 0, 5);
		gbc_panel_8.gridx = 0;
		gbc_panel_8.gridy = 0;
		panel_7.add(panel_8, gbc_panel_8);
		
		JLabel lblsire = new JLabel("Sire:");
		lblsire.setFont(new Font("Tahoma", Font.BOLD, 20));
		panel_8.add(lblsire);
		
		
		sireDisplay.setFont(new Font("Tahoma", Font.PLAIN, 20));
		panel_8.add(sireDisplay);
		
		JPanel panel_9 = new JPanel();
		GridBagConstraints gbc_panel_9 = new GridBagConstraints();
		gbc_panel_9.anchor = GridBagConstraints.NORTHWEST;
		gbc_panel_9.gridx = 1;
		gbc_panel_9.gridy = 0;
		panel_7.add(panel_9, gbc_panel_9);
		panel_9.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		JLabel lbldame = new JLabel("Dame:");
		lbldame.setFont(new Font("Tahoma", Font.BOLD, 20));
		panel_9.add(lbldame);
		
		
		dameDisplay.setFont(new Font("Tahoma", Font.PLAIN, 20));
		panel_9.add(dameDisplay);
		
		JPanel panel_10 = new JPanel();
		panel_1.add(panel_10, BorderLayout.SOUTH);
		panel_10.setLayout(new BoxLayout(panel_10, BoxLayout.Y_AXIS));
		
		JPanel panel_11 = new JPanel();
		panel_10.add(panel_11);
		
		JLabel lblOffspring = new JLabel("Offspring");
		lblOffspring.setFont(new Font("Tahoma", Font.BOLD, 20));
		panel_11.add(lblOffspring);
		
		
		panel_10.add(offspringDisplay);
		offspringDisplay.setLayout(new BoxLayout(offspringDisplay, BoxLayout.X_AXIS));
		
		search.addKeyListener(new KeyListener(){
			@Override
			public void keyTyped(KeyEvent e) {}
			@Override
			public void keyPressed(KeyEvent e) {}
			@Override
			public void keyReleased(KeyEvent e) {
				if(e.getKeyCode()==KeyEvent.VK_ENTER){
					createDisplaySearchDetails(bovineIdTxt.getText());
				}
			}
			
		});
		search.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				createDisplaySearchDetails(bovineIdTxt.getText());
			}
		});
	}
	
	protected void createDisplaySearchDetails(String searchText) 
	{
		final Table offspringPanel = new Table("Offspring List",true,
			"Visual ID","NLIS","DOB");
		
		// Disable highlighting when there is no use for it
		offspringPanel.setHeaderClickable(0, false);
		offspringPanel.setHeaderClickable(1, false);
		offspringPanel.setHeaderClickable(2, false);
		Bovine[] bList = LivestockSolutions.getBovines();
		Bovine focusBovine = new Bovine(null);
		for(Bovine b : bList)
		{
			if(b.getEartag().getEartagID().toLowerCase().contains(searchText.trim().toLowerCase())
			|| b.getEartag().getNlisID().toLowerCase().contains(searchText.trim().toLowerCase()))
			{
				focusBovine = b;
				break;
			}
			
		}
		displayVisTag.setText(focusBovine.getEartag().getEartagID());
		displayNLIStag.setText(focusBovine.getEartag().getNlisID());
		
		// Set message when there are no offspring
		offspringPanel.setEmptyMessage(displayVisTag.getText()+" has no offspring.");
		if(focusBovine.getSire()!=null)
		{
			sireDisplay.setText(focusBovine.getSire().getEartag().getEartagID());
		}
		else
		{
			sireDisplay.setText("Not Listed");
		}
		if(focusBovine.getDame()!=null)
		{
			dameDisplay.setText(focusBovine.getDame().getEartag().getEartagID());
		}
		else
		{
			dameDisplay.setText("Not Listed");
		}
		Bovine[] offspring = focusBovine.getOffspring();
		if(offspring.length > 0)
		{
			for(int i=0; i<offspring.length;i++)
			{
				offspringPanel.addRow(new JLabel(offspring[i].getEartag().getEartagID()),
						new JLabel(offspring[i].getEartag().getNlisID()),
						new JLabel(JDatePickerDate.format(offspring[i].getDob())));
				// Disable highlighting when there is no use for it
				offspringPanel.setRowClickable(false);
			}
		}
		
		offspringDisplay.add(offspringPanel);
	}
	
}
